﻿using System;
using System.Windows.Forms;
using IBNet.UtilsDataStructuresAndExtensions;

namespace IBNet.DayTrade
{
   public partial class DayTradeMainForm : Form, IWrapDayTradeMainForm
   {
      private void UpdatePortfolioListView(string symbol, int position)
      {
         listViewPortfolio.Upsert(symbol, symbol, 
            InvocationMethod.BeginInvoke, new string[] { position.ToString() });
      }

      private void RemoveFromPortfolioListView(string symbol)
      {
         listViewPortfolio.InvokeIfNeeded(delegate
         {
            listViewPortfolio.Items.RemoveByKey(symbol);
         }, InvocationMethod.Invoke);
      }

      private void UpsertAccountValueDictionaryAndListView(string key, string value)
      {
         // Upsert dictionary if value is parsable to a float.
         // e.g. value "100" is parsable to float and value "true" is not.
         // This is done because we need need to know about the account value numbers sent,
         // not the account string or true/false values sent.
         float valueAsFloat;
         if (Single.TryParse(value, out valueAsFloat))         
         {
            UpsertResult typeOfUpsertPerformed = _accountFieldToAccountValue.Upsert(key, valueAsFloat);
            switch (typeOfUpsertPerformed)
            {
               case UpsertResult.Update:
                  listViewAccountValue.UpdateValues(key, 
                     InvocationMethod.BeginInvoke, new string[] { value });
                  break;
               case UpsertResult.Insert:
                  listViewAccountValue.Insert(key, key, 
                     InvocationMethod.BeginInvoke, new string[] { value });
                  break;
            }
         }
      }

      private void IncrementListViewBarsReceivedByOne(string fullSymbol)
      {
         listViewRealTimeBarSubscriptions.InvokeIfNeeded(delegate
         {
            ListViewItem.ListViewSubItemCollection symbolSubItems =
               listViewRealTimeBarSubscriptions.Items[fullSymbol].SubItems;

            int barsReceived = Int32.Parse(symbolSubItems[1].Text);
            barsReceived++;
            // 1 == index of list view column "Bars"
            symbolSubItems[1].Text = barsReceived.ToString();

         }, InvocationMethod.BeginInvoke);
      }

      private void IncrementTradeCountOnRealTimeBarsListView(string fullSymbol)
      {
         listViewRealTimeBarSubscriptions.InvokeIfNeeded(delegate
         {
            ListViewItem.ListViewSubItemCollection subItemsForThisSymbol =
               listViewRealTimeBarSubscriptions.Items[fullSymbol].SubItems;

            // 2 == index of "Trades" column in listViewRealTimeBarSubscriptions 
            int tradeCount = Int32.Parse(subItemsForThisSymbol[2].Text);
            tradeCount++;            
            subItemsForThisSymbol[2].Text = tradeCount.ToString();

         }, InvocationMethod.BeginInvoke);
      }

      private void UpdateListViewLastPrice(string fullSymbol, decimal newLastPrice)
      {
         listViewRealTimeBarSubscriptions.InvokeIfNeeded(delegate
         {
            ListViewItem.ListViewSubItemCollection subItemsForThisSymbol =
               listViewRealTimeBarSubscriptions.Items[fullSymbol].SubItems;

            // 3 == index of "Last" (price) column in listViewRealTimeBarSubscriptions
            subItemsForThisSymbol[3].Text = newLastPrice.ToString();

         }, InvocationMethod.BeginInvoke);
      }
   }
}